package com.ruoyi.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.SProjectFee;
import com.ruoyi.base.service.ISProjectFeeService;
import com.ruoyi.base.mapper.SProjectFeeMapper;
import com.ruoyi.common.annotation.CompanyScope;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.options.domain.Option;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 姚煜峰
* @description 针对表【S_PROJECT】的数据库操作Service实现
* @createDate 2023-06-27 09:53:48
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class SProjectFeeServiceImpl extends ServiceImpl<SProjectFeeMapper, SProjectFee> implements ISProjectFeeService {

    @Autowired
    ISysDictDataService dictDataService;



    @Override
    public AjaxResult insert(SProjectFee project) {

        if(StringUtils.isEmpty(project.getFeeType())){
            return AjaxResult.error("一级费目不能为空");
        }

        if(StringUtils.isEmpty(project.getProjectFeeName())){
            return AjaxResult.error("项目名称不能为空");
        }

        if(StringUtils.isEmpty(project.getProjectType())){
            return AjaxResult.error("项目类型不能为空");
        }

        //校验 一级费目+项目名称+公司代码+项目类型 不能重复

        String companyCod = StringUtils.isNotEmpty(project.getCompanyCod())?project.getCompanyCod(): SecurityUtils.getCompanyCode();

        QueryWrapper<SProjectFee> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(SProjectFee::getFeeType, project.getFeeType())
                .eq(SProjectFee::getProjectFeeName, project.getProjectFeeName())
                .eq(SProjectFee::getProjectType, project.getProjectType())
                .eq(SProjectFee::getCompanyCod, companyCod);

        SProjectFee sProjectFee = this.getOne(queryWrapper);

        if(StringUtils.isNotNull(sProjectFee)){
            return AjaxResult.error("项目类型+一级费目+项目名称+公司代码 不能重复");
        }

        return this.save(project)?AjaxResult.success():AjaxResult.error();
    }

    @Override
    public AjaxResult update(SProjectFee project) {

        if(StringUtils.isEmpty(project.getFeeType())){
            return AjaxResult.error("一级费目不能为空");
        }

        if(StringUtils.isEmpty(project.getProjectFeeName())){
            return AjaxResult.error("项目名称不能为空");
        }

        if(StringUtils.isEmpty(project.getProjectType())){
            return AjaxResult.error("项目类型不能为空");
        }

        //校验 项目类型+一级费目+项目名称+公司代码+非本projectid 不能重复

        QueryWrapper<SProjectFee> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(SProjectFee::getFeeType, project.getFeeType())
                .eq(SProjectFee::getProjectFeeName, project.getProjectFeeName())
                .eq(SProjectFee::getCompanyCod, project.getCompanyCod())
                .eq(SProjectFee::getProjectType, project.getProjectType())
                .ne(SProjectFee::getProjectFeeId, project.getProjectFeeId());

        SProjectFee sProjectFee = this.getOne(queryWrapper);

        if(StringUtils.isNotNull(sProjectFee)){
            return AjaxResult.error("项目类型+一级费目+项目名称+公司代码 不能重复");
        }

        return this.updateById(project)?AjaxResult.success():AjaxResult.error();

    }

    @Override
    public AjaxResult label(SProjectFee project) {
        QueryWrapper<SProjectFee> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(StringUtils.isNotEmpty(project.getFeeType()), SProjectFee::getFeeType, project.getFeeType())
                .like(StringUtils.isNotEmpty(project.getProjectFeeName()), SProjectFee::getProjectFeeName, project.getProjectFeeName())
                .eq(StringUtils.isNotEmpty(project.getCompanyCod()), SProjectFee::getCompanyCod, project.getCompanyCod())
                .eq(StringUtils.isNotEmpty(project.getProjectType()), SProjectFee::getProjectType, project.getProjectType())
                .orderByAsc(SProjectFee::getCompanyCod)
                .orderByDesc(SProjectFee::getCreateTime);

        List<SProjectFee> projectList = list(queryWrapper);

        SysDictData sysDictData = new SysDictData();

        sysDictData.setDictType("fee_type");

        List<SysDictData> dictDataList = dictDataService.selectDictDataList(sysDictData);

        for(SProjectFee p : projectList){


            String feeType = p.getFeeType();

            String feeTypeName = dictDataList.stream().filter(d -> d.getDictValue().equals(feeType)).findFirst().get().getDictLabel();

            String originFeeName = p.getProjectFeeName();

            p.setProjectFeeName(feeTypeName + "_" + originFeeName + "_默认税点:" + p.getTaxRate() + "%");

        }

        return AjaxResult.success(projectList);
    }

    @Override
    public AjaxResult option(SProjectFee project) {
        QueryWrapper<SProjectFee> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(StringUtils.isNotEmpty(project.getFeeType()), SProjectFee::getFeeType, project.getFeeType())
                .like(StringUtils.isNotEmpty(project.getProjectFeeName()), SProjectFee::getProjectFeeName, project.getProjectFeeName())
                .eq(StringUtils.isNotEmpty(project.getCompanyCod()), SProjectFee::getCompanyCod, project.getCompanyCod())
                .eq(StringUtils.isNotEmpty(project.getProjectType()), SProjectFee::getProjectType, project.getProjectType())
                .orderByAsc(SProjectFee::getCompanyCod)
                .orderByDesc(SProjectFee::getCreateTime);

        List<SProjectFee> projectList = baseMapper.selectList(queryWrapper);

        List<Option> options = new ArrayList<>();

        SysDictData sysDictData = new SysDictData();

        sysDictData.setDictType("fee_type");

        List<SysDictData> dictDataList = dictDataService.selectDictDataList(sysDictData);

        for(SProjectFee p : projectList){

            Option option = new Option();

            String feeType = p.getFeeType();

            String feeTypeName = dictDataList.stream().filter(d -> d.getDictValue().equals(feeType)).findFirst().get().getDictLabel();

            String originFeeName = p.getProjectFeeName();

            option.setLabel(feeTypeName + "_" + originFeeName);
            option.setValue(String.valueOf(p.getProjectFeeId()));
            option.setKey(String.valueOf(p.getProjectFeeId()));

            options.add(option);

        }

        return AjaxResult.success(options);
    }
}




